package com.cskaoyan.controller.wx;


import com.cskaoyan.bean.BaseRespVo;

import com.cskaoyan.bean.UserInfoBean;
import com.cskaoyan.bean.WxLoginUserData;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.config.aliiyun.AliyunComponent;
import com.cskaoyan.config.shiro.MallToken;
import com.cskaoyan.service.wx.WxAuthService;
import com.cskaoyan.util.PhoneUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ClassName: WxAuthController
 * Description:
 * date: 2022/6/7 20:06
 *
 * @author bruce
 * @since JDK 8
 */
@RestController
@RequestMapping("wx/auth")
public class WxAuthController {
    @Autowired
    AliyunComponent aliyunComponent;

    @Autowired
    WxAuthService wxAuthService;

    @RequestMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String passwordOld = (String) map.get("password");

        String password = DigestUtils.md5DigestAsHex(passwordOld.getBytes());
        System.out.println(password);
        Subject subject = SecurityUtils.getSubject();
        //subject.login(new UsernamePasswordToken(username, password));
        try {
            subject.login(new MallToken(username, password, "wx"));

            // 登录之后就可以通过subject来获得信息了
            boolean authenticated = subject.isAuthenticated();
            System.out.println(authenticated);

            // 下面这部分值应该来源于数据库 → 要知道用户信息principal
            // 你在realm的doGetAuthorizationInfo中的第一个参数放入的是什么，取出的就是什么
            MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            // 根据marketAdmin的信息来完善下面的信息

            WxLoginUserData wxLoginUserData = new WxLoginUserData();
            UserInfoBean userInfo = new UserInfoBean();
            userInfo.setAvatarUrl(primaryPrincipal.getAvatar());
            userInfo.setNickName(primaryPrincipal.getUsername());
            wxLoginUserData.setUserInfo(userInfo);
            // loginUserData.setToken("c4d988f3-e4c5-4e46-b6ce-13b9e008ea20");
            String id = (String) subject.getSession().getId();
            wxLoginUserData.setToken(id);
            return BaseRespVo.ok(wxLoginUserData);
        } catch (AuthenticationException e) {
            return BaseRespVo.unlogin();
        }
    }

    @PostMapping("logout")
    public BaseRespVo logout() {
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }
private static String code2;
private static String mobile2;

    @RequestMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map map,HttpServletRequest request) {
        String mobile = (String) map.get("mobile");
        // HttpSession session = request.getSession();
        if (PhoneUtil.isMobileNO(mobile)) {
            //获取一个验证码
            String code = String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, 5)));
            aliyunComponent.sendMsg(mobile, code);
            code2=code;
            mobile2=mobile;
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.wrongParam();
        }
    }

    @RequestMapping("register")
    public BaseRespVo register(@RequestBody Map map) {

        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String code = (String) map.get("code");
        String mobile = (String) map.get("mobile");
        String wxCode = (String) map.get("wxCode");

        if (code.equals(code2)&& mobile.equals(mobile2)){
            if(wxAuthService.judgeUsername(username)){
                if (wxAuthService.judgePhone(mobile)){
                    wxAuthService.insertNewUser(username,password,mobile,wxCode);
                    String passwordNew = DigestUtils.md5DigestAsHex(password.getBytes());
                    Subject subject = SecurityUtils.getSubject();
                    //subject.login(new UsernamePasswordToken(username, password));
                    try {
                        subject.login(new MallToken(username, passwordNew, "wx"));

                        // 登录之后就可以通过subject来获得信息了
                        boolean authenticated = subject.isAuthenticated();
                        System.out.println(authenticated);

                        // 下面这部分值应该来源于数据库 → 要知道用户信息principal
                        // 你在realm的doGetAuthorizationInfo中的第一个参数放入的是什么，取出的就是什么
                        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
                        // 根据marketAdmin的信息来完善下面的信息

                        WxLoginUserData wxLoginUserData = new WxLoginUserData();
                        UserInfoBean userInfo = new UserInfoBean();
                        userInfo.setAvatarUrl(primaryPrincipal.getAvatar());
                        userInfo.setNickName(primaryPrincipal.getUsername());
                        wxLoginUserData.setUserInfo(userInfo);
                        // loginUserData.setToken("c4d988f3-e4c5-4e46-b6ce-13b9e008ea20");
                        String id = (String) subject.getSession().getId();
                        wxLoginUserData.setToken(id);
                        return BaseRespVo.ok(wxLoginUserData);
                    } catch (AuthenticationException e) {
                        return BaseRespVo.unlogin();
                    }
                }else {
                    return BaseRespVo.phoneExist();
                }
            }else {
                return BaseRespVo.usernameExist();
            }
        }else {
            return BaseRespVo.wrongCode();
        }
    }

    @RequestMapping("reset")
    public BaseRespVo reset(@RequestBody Map map) {
        String mobile = (String) map.get("mobile");
        String code = (String) map.get("code");
        String password = (String) map.get("password");
        if (PhoneUtil.isMobileNO(mobile)) {
            if (code.equals(code2) && mobile.equals(mobile2)) {
                wxAuthService.resetPassword(mobile, password);
                return BaseRespVo.ok();
            } else {
                return BaseRespVo.wrongCode();
            }
        } else {
            return BaseRespVo.wrongParam();
        }
    }
}

